package com.ruoyi.hr.personalFile.controller;

import java.util.List;

import com.ruoyi.hr.personalFile.domain.HrPersonnelFile;
import com.ruoyi.hr.personalFile.service.IHrPersonnelFileService;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人事档案Controller
 *
 * @author xzshan
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/hr/personalFile")
public class HrPersonnelFileController extends BaseController {
  private String prefix = "hr/personalFile";

  @Autowired
  private IHrPersonnelFileService hrPersonnelFileService;

  @RequiresPermissions("hr:personalFile:view")
  @GetMapping()
  public String personalFile() {
    return prefix + "/personalFile";
  }

  /**
   * 查询人事档案列表
   */
  @RequiresPermissions("hr:personalFile:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(HrPersonnelFile hrPersonnelFile) {
    startPage();
    List<HrPersonnelFile> list = hrPersonnelFileService.selectHrPersonnelFileList(hrPersonnelFile);
    return getDataTable(list);
  }

  /**
   * 导出人事档案列表
   */
  @RequiresPermissions("hr:personalFile:export")
  @Log(title = "人事档案", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(HrPersonnelFile hrPersonnelFile) {
    List<HrPersonnelFile> list = hrPersonnelFileService.selectHrPersonnelFileList(hrPersonnelFile);
    ExcelUtil<HrPersonnelFile> util = new ExcelUtil<HrPersonnelFile>(HrPersonnelFile.class);
    return util.exportExcel(list, "personalFile");
  }

  @Log(title = "人事管理", businessType = BusinessType.IMPORT)
  @PostMapping("/importData")
  @ResponseBody
  public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
    ExcelUtil<HrPersonnelFile> util = new ExcelUtil<HrPersonnelFile>(HrPersonnelFile.class);
    List<HrPersonnelFile> personnelFiles = util.importExcel(file.getInputStream());
    String operName = ShiroUtils.getSysUser().getLoginName();
    String message = hrPersonnelFileService.importPerson(personnelFiles, updateSupport, operName);
    return AjaxResult.success(message);
  }

  @GetMapping("/importTemplate")
  @ResponseBody
  public AjaxResult importTemplate() {
    ExcelUtil<HrPersonnelFile> util = new ExcelUtil<HrPersonnelFile>(HrPersonnelFile.class);
    return util.importTemplateExcel("人事数据");
  }


  /**
   * 新增人事档案
   */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add";
  }

  /**
   * 新增保存人事档案
   */
  @RequiresPermissions("hr:personalFile:add")
  @Log(title = "人事档案", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(HrPersonnelFile hrPersonnelFile) {
    return toAjax(hrPersonnelFileService.insertHrPersonnelFile(hrPersonnelFile));
  }

  /**
   * 修改人事档案
   */
  @GetMapping("/edit/{id}")
  public String edit(@PathVariable("id") Long id, ModelMap mmap) {
    HrPersonnelFile hrPersonnelFile = hrPersonnelFileService.selectHrPersonnelFileById(id);
    mmap.put("hrPersonnelFile", hrPersonnelFile);
    return prefix + "/edit";
  }

  /**
   * 修改保存人事档案
   */
  @RequiresPermissions("hr:personalFile:edit")
  @Log(title = "人事档案", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(HrPersonnelFile hrPersonnelFile) {
    return toAjax(hrPersonnelFileService.updateHrPersonnelFile(hrPersonnelFile));
  }

  /**
   * 删除人事档案
   */
  @RequiresPermissions("hr:personalFile:remove")
  @Log(title = "人事档案", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {
    return toAjax(hrPersonnelFileService.deleteHrPersonnelFileByIds(ids));
  }
}
